#include "playscene.h"
#include "chooselevelscene.h"
#include "mycoin.h"
#include "dataconfig.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QPixmap>
#include <QLabel>
#include <QStyle>
#include <QPicture>
#include <QPropertyAnimation>
PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
{

}
void PlayScene::initScene()
{
    ChooseLevelScene* myp = dynamic_cast<ChooseLevelScene*>(parent());
    if(myp)
    {
        this->setFixedSize(myp->width(),myp->height());
    }
    QMenuBar* mbar =  this->menuBar();
    QMenu* mn = mbar->addMenu("开始");

    QAction * action = mn->addAction("退出");
    connect(action,&QAction::triggered,[=](){
        this->close();
    });

    this->setWindowTitle(QString("欢迎来到第%1关").arg(level));
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //关卡标签
    QLabel* label = new QLabel(QString("level:%1").arg(level),this);
    QFont font;
    font.setFamily("华文新魏");
    font.setBold(true);
    font.setPointSize(20);
    label->setFont(font);
    label->move(20,this->height()-label->height());
    label->setGeometry(QRect(30, this->height() - 50,140, 50));
    //金币的背景
    for(int i = 0;i<4;i++)
    {
        for (int j = 0;j<4;j++)
        {
            QLabel* label = new QLabel(this);
            QPixmap pic;
            pic.load(":/res/BoardNode(1).png");
            label->setPixmap(pic);
            label->setGeometry(0,0,50,50);
            label->move(120+i*50,280+j*50);

        }
    }
    //各个金币按钮
    initCoinBtn();
    //胜利图片
    initWinImg();
}

void PlayScene::initCoinBtn()
{
    dataConfig dc;
    const QMap<int, QVector< QVector<int> > > & mData  = dc.mData;
    //创建金币按钮
    for(int i = 0;i<4;i++)
    {
        for (int j = 0;j<4;j++)
        {
            QString img = ":/res/Coin0001.png";
            qDebug()<<"mData[level][i][j] = "<<mData[level][i][j];
            bool f_lag = true;
            if(!mData[level][i][j])
            {
                img = ":/res/Coin0008.png";
                f_lag = false;
            }
            MyCoin * btn_coin = new MyCoin(img);
            vec_btn.push_back(btn_coin);
            gameArray[i][j] = (int)btn_coin;
            btn_coin->setFlag(f_lag);
            btn_coin->setPx(i);
            btn_coin->setPy(j);
            btn_coin->setParent(this);
            btn_coin->setGeometry(0,0,50,50);
            btn_coin->move(122+i*50,283+j*50);
            connect(btn_coin,&QPushButton::clicked,[=](){
                qDebug()<<"点击了金币"<<btn_coin->print();
                //展示翻转动画
                btn_coin->changeflag();
                //影响旁边的按钮动画
                calcOtherBtn(btn_coin->getPx(),btn_coin->getPy());
                //是否通关
                if(win())
                {
                   //通关
                    qDebug()<<"通关了";
                    doWinAction();
                }
            });

        }
    }
}
bool PlayScene::win()
{
    for (auto it = this->vec_btn.begin();it!=vec_btn.end();it++)
    {
        if( !(*it)->getflag())
        {
            return  false;
        }
    }
    for (auto it = this->vec_btn.begin();it!=vec_btn.end();it++)
    {
       (*it)->isWin();
    }
    return true;
}
void PlayScene::calcOtherBtn(int x,int y)
{
//    [x-1][y]
//    [x+1][y]
//    [x][y-1]
//    [x][y+1]
    MyCoin*btn = nullptr;
    if(x-1>=0)
    {
        btn = (MyCoin*)gameArray[x-1][y];
        btn->changeflag();
    }
    if(x+1<=3)
    {
        btn = (MyCoin*)gameArray[x+1][y];
        btn->changeflag();
    }
    if(y-1>=0)
    {
        btn = (MyCoin*)gameArray[x][y-1];
        btn->changeflag();
    }
    if(y+1<=3)
    {
        btn = (MyCoin*)gameArray[x][y+1];
        btn->changeflag();
    }
}
void PlayScene::initWinImg()
{
    label = new QLabel(this);
    QPixmap pix;
    pix.load(":/res/LevelCompletedDialogBg.png");
    label->setPixmap(pix);
    label->setFixedSize(pix.width(),pix.height());
    label->move(pix.width()/2-20,22);
    label->hide();
}
void PlayScene::paintEvent(QPaintEvent* ev)
{
    QPainter pter(this);

    QPixmap *pix = new QPixmap(":/res/PlayLevelSceneBg.png");
    pter.drawPixmap(0,0,this->width(),this->height(),*pix);
    pix->load(":/res/Title.png");
    QPixmap psc = pix->scaled(pix->width()*0.5,pix->height()*0.5);
    pter.drawPixmap(0,this->menuBar()->height(),psc.width(),psc.height(),psc);

    delete pix;
}
void PlayScene::doWinAction()
{
    label->show();
    QPropertyAnimation * anim = new QPropertyAnimation(label,"geometry");
    anim->setDuration(500);

    anim->setStartValue(QRect(label->x(),label->y(),label->width(),label->height()));
    anim->setEndValue(QRect(label->x(),label->y()+ 100,label->width(),label->height()));

    anim->setEasingCurve(QEasingCurve::OutBounce);
    anim->start();
}
